home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / PARSING.SWG / 0001_PARSENUM.PAS.pas next >
Pascal/Delphi Source File  |  1993-05-28  |  7KB  |  208 lines

  1. Type
  2.    RW_toKEN = Record
  3.       token_str :String[9];
  4.       token_cod :toKEN_CODE;
  5.    end;
  6.  
  7.    RW_Type = Array[0..9] of RW_toKEN;
  8.    RWT_PTR = ^RW_Type;
  9.  
  10. Const
  11.    NULL = '';
  12.  
  13.    Rw_2  :RW_Type = ((token_str : 'do'; token_cod : tdo),
  14.                      (token_str : 'if'; token_cod : tif),
  15.                      (token_str : 'in'; token_cod : tin),
  16.                      (token_str : 'of'; token_cod : tof),
  17.                      (token_str : 'or'; token_cod : tor),
  18.                      (token_str : 'to'; token_cod : tto),
  19.                      (token_str : NULL; token_cod : NO_toKEN),
  20.                      (token_str : NULL; token_cod : NO_toKEN),
  21.                      (token_str : NULL; token_cod : NO_toKEN),
  22.                      (token_str : NULL; token_cod : NO_toKEN)
  23.                     );
  24.  
  25.     ...the difference being the explicit declaration of the Constant
  26.     Record fields. (I'm used to Array Constants, not Record
  27.     Constants - I was unaware of the requirement)
  28.  
  29.     PARSinG NUMBERS
  30.  
  31.     Now we'll concentrate on parsing Integer and Real numbers.
  32.  
  33.     The Pascal definition of a number begins With an UNSIGNED
  34.     Integer. An unsigned Integer consists of one or more consecutive
  35.     DIGITS. The simplest Form of a number token is an unsigned
  36.     Integer:
  37.  
  38.     1 9 120 12654
  39.  
  40.     A number token can also be an unsigned Integer (the whole part)
  41.     followed by a fraction part. A fraction part consists of a
  42.     decimal point followed by an unsigned Integer, such as:
  43.  
  44.     123.45 0.9987564
  45.  
  46.     These numbers have whole parts 123 and 0 respectively, and
  47.     fraction parts .45 and .9987564 respectively.
  48.  
  49.     A number token can also be a whole part followed by an EXPONENT
  50.     part. An exponent part consists of an "E" (or "e") followed by
  51.     an unsigned Integer. An optional exponent sign, + or -, can
  52.     appear between the letter and the first exponent digit.
  53.     Examples:
  54.  
  55.     134e2  2E99 123e-45 73623E+4
  56.  
  57.     Finally, a number token can be a whole part followed by a
  58.     fraction part and an exponent part, in that order:
  59.  
  60.     2.3498E7 0.00034e-66
  61.  
  62.     I arbitrarily limit the number of digits to 20, and the exponent
  63.     value from -37 to +37 - the exact value necessary to limit this
  64.     value is dependant on how Real values are represented on the
  65.     Computer.
  66.  
  67.     The "get_number" Function is likely to be the biggest Function
  68.     in your scanner, but it should be relatively straighForward to
  69.     code...in light of what has already been done With the scanner/
  70.     tokenizer module, and the definition of a number.
  71.  
  72.     EXERCISE #1
  73.  
  74.     Write the get_number Function to parse Integers and Real
  75.     numbers.
  76.  
  77.     You will need to add the following Types and Variables to your
  78.     global data segment:
  79.  
  80.     Type  { add "Real"s to list... }
  81.  
  82.     LITERAL_Type = (Integer_LIT, Real_LIT, String_LIT);
  83.  
  84.     LITERAL_REC = Record
  85.        Case lType:LITERAL_Type of
  86.           Integer_LIT: (ivalue :Integer);
  87.           Real_LIT   : (rvalue :Real   );
  88.           String_LIT : (svalue :String );
  89.     end;
  90.  
  91.     Var
  92.  
  93.     digit_count :Word;
  94.     count_error :Boolean;
  95.  
  96. --------------     PART 2     ---------------------------------------
  97.  
  98.     The rest of this post will cover two simple topics - parsing
  99.     Strings inside quotes, and parsing comments.
  100.  
  101.     PARSinG COMMENTS {}
  102.  
  103.     The Compiler should ignore the input between two curly braces
  104.     ({}), and the curly braces themselves. My scanner is written so
  105.     the entire comment is replace by a Single blank (" "), although
  106.     you could possibly Write the scanner so that comments are
  107.     _totally_ ignored.
  108.  
  109.     EXERCISE #2:
  110.  
  111.     Integrate COMMENT detection into the get_Char routine, so that
  112.     when your Character fetching routine will ignore comments and
  113.     pass a blank when a comment is encountered, skipping the comment
  114.     entirely For the next fetch.
  115.  
  116.     Make sure that the routine keeps reading Until the right curly
  117.     brace is detected, even past the end-of-line. if the end-of-File
  118.     is encountered beFore the right curly brace is found, an
  119.     "unexpected end" error should be generated.
  120.  
  121.     PARSinG StringS (QUOTES) ''
  122.  
  123.     The quote Character delimits Strings, any Character between the
  124.     Strings is ignored by the Compiler, except to stored as a String
  125.     LITERAL. if you wish a ' (quote) to be included in the literal,
  126.     and extra ' must precede it.
  127.  
  128.     One possible tricky area is the {} (comment) Character. You must
  129.     be careful not to inadvertently trigger the comment routine within
  130.     the quote routine While reading a String, otherwise you will
  131.     have a BUG.
  132.  
  133.     EXERCISE #3:
  134.  
  135.     Add a quote routine to the get_token routine within your module,
  136.     to fetch Strings, as a LITERAL IDENTifIER when the QUOTE
  137.     Character is detected.
  138.  
  139.     The following mods to your Types are required:
  140.  
  141.     Eof_Char = #$7F;
  142.  
  143. Type
  144.   Char_CODE  = (LETTER, DIGIT, QUOTE, SPECIAL, Eof_CODE);
  145.  
  146.  {  The following code init's the Character maping table:  }
  147.  
  148. Var
  149.    ch :Byte;
  150. begin
  151.    For ch := 0 to 255 do
  152.       Char_table[ch] := SPECIAL;
  153.    For ch := ord('0') to ord('9') do
  154.       Char_table[ch] := DIGIT;
  155.  
  156.    For ch := ord('A') to ord('Z') do
  157.       Char_table[ch] := LETTER;
  158.    For ch := ord('a') to ord('z') do
  159.       Char_table[ch] := LETTER;
  160.  
  161.    Char_table[ord(Eof_Char)] := Eof_CODE;
  162.  
  163.    Char_table[39] := QUOTE;
  164. end;
  165.  
  166.     ----------------------------------------------------------------
  167.  
  168.     PLEASE, please let me know what you think about these posts,
  169.     even if they're negative - I want to have some feedback on the
  170.     difficulties, and whether or not people are having trouble
  171.     following the material - I _can_ be more concise at the cost of
  172.     being more verbose - if it's needed!
  173.  
  174.     if you are having problems With your source code, and want me to
  175.     do a detailed examination of your code, expecially if it's
  176.     written in a language other than Pascal, send me email via the
  177.     Internet - to avoid "carpet bombing" the conference with
  178.     undesired material.
  179.  
  180.  
  181.     NEXT POST:
  182.  
  183.     Error codes, and putting your code to the test - our first
  184.     utility (other than the lister) : a source Program Compactor
  185.     (not cruncher).
  186.  
  187.     FUTURE POSTS:
  188.  
  189.     - Review and (hopefully) a status report from "students"
  190.     - Symbol table
  191.     - YA utility (cross - referencer)
  192.     - YA utility (source Program CRUNCHer)
  193.     - YA utility (source Program UNcruncher)
  194.     - Parsing simple expressions
  195.     - Utility : CALC, using infix-to-postfix conversions and stack
  196.       ops.
  197.     - Parsing statements
  198.     - Utility: Pascal syntax checker part I
  199.     - Parsing declarations (Var, Type, etc)
  200.       incl's: much improved (and much more Complex) symbol table
  201.     - Utility: Declarations analyzer.
  202.     - Syntax Checker part II
  203.     - Parsing Program, Procedure, and Function declarations
  204.       (routines).
  205.     - Syntax checker Part III
  206.  
  207.     - Review and discussion?
  208.